<template>
  <div class="workflow-bind-page">
    <h2>流程-表单绑定管理</h2>
    <el-button type="primary" @click="onBind" v-if="hasPermission(WorkflowAuth.PROCESS_FORM_BIND_ADD)">新建绑定</el-button>
    <el-table :data="tableData" :loading="loading" style="width: 100%" @selection-change="onSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="processName" label="流程名称" />
      <el-table-column prop="formName" label="表单名称" />
      <el-table-column prop="status" label="状态" />
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button size="small" type="danger" @click="onUnbind(row)" v-if="hasPermission(WorkflowAuth.PROCESS_FORM_BIND_DEL)">解绑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :total="total"
      @current-change="fetchList"
      @size-change="fetchList"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      style="margin-top: 16px;"
    />
    <!-- 新建绑定弹窗 -->
    <el-dialog v-model="bindVisible" title="新建绑定" width="400px">
      <el-form :model="bindForm" :rules="bindRules" ref="bindFormRef" label-width="80px">
        <el-form-item label="流程" prop="processId">
          <el-select v-model="bindForm.processId" filterable placeholder="请选择流程">
            <el-option v-for="item in processOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="表单" prop="formId">
          <el-select v-model="bindForm.formId" filterable placeholder="请选择表单">
            <el-option v-for="item in formOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="bindVisible = false">取消</el-button>
        <el-button type="primary" @click="onBindSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { getProcessFormBindList, addProcessFormBind, deleteProcessFormBinds } from '@/api/workflow/processFormBind.api'
import { getProcessList } from '@/api/workflow/process.api'
import { getFormList } from '@/api/workflow/form.api'
import { WorkflowAuth } from '@/auth/workflow/workflow.auth'
import { usePermission } from '@/hooks/web/usePermission'
import { ElMessage } from 'element-plus'
import Icon from '@/components/Icon/Icon.vue'

// 引入统一的夜间模式样式
import '@/components/Workflow/styles/dark-theme.css'

const { hasPermission } = usePermission()

const tableData = ref([])
const loading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const selectedRows = ref([])

const bindVisible = ref(false)
const bindForm = ref({ processId: '', formId: '' })
const bindFormRef = ref()
const processOptions = ref([])
const formOptions = ref([])
const bindRules = {
  processId: [{ required: true, message: '请选择流程', trigger: 'change' }],
  formId: [{ required: true, message: '请选择表单', trigger: 'change' }]
}

function fetchList() {
  loading.value = true
  getProcessFormBindList({ pageNum: pageNum.value, pageSize: pageSize.value }).then(res => {
    console.log(res);
    
    tableData.value = res || []
    total.value = res?.total || 0
  }).finally(() => loading.value = false)
}
function fetchOptions() {
  getProcessList({ pageNum: 1, pageSize: 100 }).then(res => {
    processOptions.value = res?.items || res?.data?.rows || []
  })
  getFormList({ pageNum: 1, pageSize: 100 }).then(res => {
    formOptions.value = res?.items || res?.data?.rows || []
  })
}
function onSelectionChange(val) { selectedRows.value = val }
function onBind() {
  bindForm.value = { processId: '', formId: '' }
  bindVisible.value = true
  fetchOptions()
}
function onBindSave() {
  bindFormRef.value.validate((valid) => {
    if (!valid) return
    addProcessFormBind({ processId: bindForm.value.processId, formId: bindForm.value.formId }).then(() => {
      bindVisible.value = false
      ElMessage.success('绑定成功')
      fetchList()
    })
  })
}
function onUnbind(row) {
  deleteProcessFormBinds([row.id]).then(() => {
    ElMessage.success('解绑成功')
    fetchList()
  })
}
onMounted(() => {
  fetchList();
});
</script>
<style scoped>
.workflow-bind-page { padding: 24px; }
</style>
